/*
 * SelectCourseInterFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jakey.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.jakey.dao.CourseDao;
import com.jakey.dao.SelectionDao;
import com.jakey.model.Course;
import com.jakey.model.Selection;
import com.jakey.util.DbUtil;

import Message.Message;
import client.TheClient;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Dimension;
import java.awt.Color;


/**
 *
 * @author  __USER__
 */
public class TeacherCourseInterFrm2 extends javax.swing.JInternalFrame {
	
	CourseDao courseDao = new CourseDao();
	SelectionDao selectionDao = new SelectionDao();
	private int capacity;
	private int numSelected;
	private int courseId=-1;

	/** Creates new form SelectCourseInterFrm */
	public TeacherCourseInterFrm2() {
		//getContentPane().setBackground(new Color(64, 116, 52));
		getContentPane().setBackground(new Color(240,240,240));
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());

	}

	private void fillTable(Course course) {
		
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		String tp="SHOW_READERS";
		Course course1 = new Course();
		Message message=new Message(tp,course1);
		message.setNumber(555);
		message.setCheckCode(6);
		
		client.TheClient client=new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for(int i=0;i<TbB.size();i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				v.add(temp.getCapacity());
				v.add(temp.getNumSelected());
				dtm.addRow(v);

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		courseTable = new javax.swing.JTable();
		jb_underFull = new javax.swing.JButton();
		jb_confirm = new javax.swing.JButton();

		setClosable(true);
		setIconifiable(true);
		setTitle("\u6559\u5E08\u8BFE\u7A0B\u9009\u62E9");

		courseTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "�γ̱��", "�γ�����", "�Ͽ�ʱ��", "�ο���ʦ", "�γ�����",
						"��ѡ����" }) {
			boolean[] canEdit = new boolean[] { false, false, false, true,
					true, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		courseTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				courseTableMousePressed(evt);
			}
		});
		jScrollPane1.setViewportView(courseTable);

		jb_underFull
				.setText("\u67E5\u770B\u6559\u52A1\u8BFE\u7A0B");
		jb_underFull.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_underFullActionPerformed(evt);
			}
		});

		jb_confirm.setText("\u786e\u8ba4\u9009\u8bfe");
		jb_confirm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_confirmActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
					.addGap(148)
					.addComponent(jb_underFull)
					.addPreferredGap(ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
					.addComponent(jb_confirm)
					.addGap(135))
				.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
					.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addGap(26)
					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 292, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jb_confirm)
						.addComponent(jb_underFull))
					.addGap(33))
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jb_underFullActionPerformed(java.awt.event.ActionEvent evt) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		String tp="UNDERFULL_LIST";
		Message message=new Message();
		message.setCheckCode(6);
		message.setType(tp);
		client.TheClient client=new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for(int i=0;i<TbB.size();i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				v.add(temp.getCapacity());
				v.add(temp.getNumSelected());
				dtm.addRow(v);

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void courseTableMousePressed(java.awt.event.MouseEvent evt) {
//		int row = courseTable.getSelectedRow();
//
//		courseId = Integer.parseInt((String) courseTable.getValueAt(row, 0));
//		capacity = Integer.parseInt((String) courseTable.getValueAt(row, 4));
//		numSelected = Integer.parseInt((String) courseTable.getValueAt(row, 5));

		int count=courseTable.getSelectedRow();
		String a=courseTable.getValueAt(count, 0).toString();
		String b=courseTable.getValueAt(count, 1).toString();
		String c=courseTable.getValueAt(count, 2).toString();
		String d=courseTable.getValueAt(count, 3).toString();
		String e=courseTable.getValueAt(count, 4).toString();
		String f=courseTable.getValueAt(count, 5).toString();
		int courseId = Integer.parseInt(a);
	}

	private void jb_confirmActionPerformed(java.awt.event.ActionEvent evt) {
		int count=courseTable.getSelectedRow();
		String a=courseTable.getValueAt(count, 0).toString();
		String b=courseTable.getValueAt(count, 1).toString();
		String c=courseTable.getValueAt(count, 2).toString();
		String d=courseTable.getValueAt(count, 3).toString();
		String ee=courseTable.getValueAt(count, 4).toString();
		String f=courseTable.getValueAt(count, 5).toString();
		int courseId = Integer.parseInt(a);
		if (courseId==-1) {
			JOptionPane.showMessageDialog(this, "请选择一门课程！");
			return;
		}
		if (ee == f) {
			JOptionPane.showMessageDialog(this, "该课程已满，请选择其他课程！.");
			return;
		}

		int n = JOptionPane.showConfirmDialog(this, "确定要选择这门课程吗?");
		if (n == 0) {
		
			    int currentSno = 9018125;
			    Selection selection = new Selection(courseId, currentSno);
			
				Message message = new Message();
				message.setAllembracing(selection);
				message.setType("SELECTION_ADD");
	            message.setCheckCode(6);			
				client.TheClient client = new TheClient();
			
				Message recevied = new Message();
			try {
				recevied = client.sendAndReceive(message);
				if (recevied.isResponse()) {
					JOptionPane.showMessageDialog(this, "选课成功!");
					this.fillTable(new Course());
				} else {
					JOptionPane.showMessageDialog(this, "选课成功!");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "已选过该门课程!");
			} finally {
				try {
				
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTable courseTable;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JButton jb_confirm;
	private javax.swing.JButton jb_underFull;
	// End of variables declaration//GEN-END:variables

}